package com.kai;

import com.kai.mapper.EmpMapper;
import com.kai.pojo.Emp;
import com.kai.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * Created By ShaoXikai on 2022/5/20
 */
public class ContextTest {

    // 通过封装的工具类拿到sqlSession
    private final SqlSession sqlSession = MybatisUtil.getSqlSession();
    // 通过sqlSession对象拿到mapper实例
    private final EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);

    // 查询所有
    @Test
    public void testSelectList() {
        List<Emp> emps = empMapper.selectList();
        for (Emp emp : emps) {
            System.out.println(emp);
        }
    }

    // 根据id查询
    @Test
    public void testSelectById() {
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.selectById(9L);
        System.out.println(emp);
    }

    // 添加
    @Test
    public void testAddEmp() {
        int i = empMapper.addEmp(new Emp(11L, "xiao", "传销", 6666666.6));
        if (i == 1) {
            System.out.println("Add OK!");
        } else {
            System.out.println("NO!");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    // 根据id删除
    @Test
    public void testDeleteById() {
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        int i = empMapper.deleteById(10L);
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("NO");
        }
        sqlSession.commit();
        sqlSession.close();
    }

    // 根据id更新信息
    @Test
    public void testUpdate() {
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = new Emp();
        emp.setId(11L);
        emp.setName("卢本伟");
        emp.setJob("牛逼");
        int i = empMapper.updateById(emp);
        if (i == 1) {
            System.out.println("OK");
        } else {
            System.out.println("ON");
        }
        sqlSession.commit();
        sqlSession.close();
    }
}
